import { defineStore } from "pinia";
import { postLogin, getLogout, getuserInfo } from '../api/api2/login'
import { userMode, userLogMode } from "../api/api2/model/loginMode";

export const useUserInfoStore = defineStore('user', {
  state: () => {
    return {
      token: localStorage.getItem('token'),
      userName: '',
      headImg: '',
      passwd: ''
    }
  },
  actions: {
    async postLoginAsync(user: userLogMode) {
      const { token, name } = await postLogin(user)
      // console.log(result)
      this.token = token
      // this.name = name
      localStorage.setItem('token', token)
      // localStorage.setItem('name', name)
    },
    async getLogoutAsync() {
      await getLogout();
      this.token = '';
      // this.name = '';
      localStorage.removeItem('token')
      // localStorage.removeItem('name')
    },
    async getuserInfoAsync() {
      const result = await getuserInfo()
      this.headImg = result.headImg
      this.passwd = result.passwd
      this.userName = result.name
    },
    reset() {
      this.headImg = ''
      this.passwd = ''
      this.userName = ''
      this.token = ''
      localStorage.removeItem('token')
    }

  }
})